import java.util.ArrayList;

/* 
 * Type:
 * 1 - tree 
 * 2 - water
 * 3 - other
 * 
 * State:
 * 
 * 1 - not burning
 * 2 - starting to burn
 * 3 - burning
 * 4 - burned
 */


public class Point {
        private ArrayList<Point> neighbors;
        private int currentState;
        int nextState;
        private int numStates = 6;
        private int type;
        private int humidity;
//      private int wind;   //kierunek wiatru 1-8
//      private double wind_p = 9; //sila wiatru
        private double t = 15;
        private double humidity_air = 90;
        int x, y;
        
        public Point() {
                  currentState = 0;
                  nextState = 0;
                  neighbors = new ArrayList<Point>();
                  type = 100;
        }
        
        public void setWindPow(Integer wPow){
        //      wind_p = wPow;
        }
        
        public void setWindPow(String s){
                
        }

        public void clicked() {
                currentState = 2; 
        }
 
        public int getState() {
                return currentState;
        }

        public int getType(){
                return type;
        }

        public void setState(int s) {
                currentState = s;
        }

        public void setType(int t){
                type = t;
        }

        public void calculateNewState(int wind_p, int wind_d) {
//              int wind = wind_d;
                if(type == 1){
                        if(this.currentState == 1){
                                        if(activeCount() > 0){
                                                double prob = Math.random();
                                                double prog, wind_wsp, temp_wsp, humadity_wsp;
                                                wind_wsp = wiatr(wind_d)*wind_p;
                                                temp_wsp = temp(t);
                                                humadity_wsp = humadity_a(humidity_air);
                                                switch(activeCount()) {
                                                        case 1:
                                                                prog = 0.1 + wind_wsp + temp_wsp + humadity_wsp;
                                                                break;
                                                        case 2:
                                                                prog = 0.15 + wind_wsp + temp_wsp + humadity_wsp;
                                                                break;
                                                        case 3:
                                                                prog = 0.25 + wind_wsp + temp_wsp + humadity_wsp;
                                                                break;
                                                        default:
                                                                prog = 0.3 + wind_wsp + temp_wsp + humadity_wsp;
                                                }
                                                if(this.humidity < 70 & prob < prog ){
                                                        this.nextState = 2;
                                                }else{
                                                        this.humidity--; 
                                                        this.nextState = 1;
                                                }
                                        }else{
                                                this.humidity--;
                                                this.nextState = 1;
                                        } 
                        }else if(this.currentState == 2){
                                this.humidity--;
                                if(this.humidity < 50)
                                        this.nextState = 3;
                                else
                                        nextState = currentState;
                        }else if(this.currentState == 3){
                                this.humidity--;
                                if(this.humidity < 30)
                                        this.nextState = 4;
                                else{
                                        nextState = currentState;
                                }
                        }else if(this.currentState == 4){
                                this.humidity--;
                                if(this.humidity < 15)
                                        this.nextState = 5;
                                else{
                                        nextState = currentState;
                                }
                        }else if(this.currentState == 5){
                                this.humidity--;
                                if(this.humidity < 5)
                                        this.nextState = 6;
                                else{
                                        nextState = currentState;
                                }
                        }
                }
        }
        

        public void changeState() {
                currentState = nextState;
        }

        public void addNeighbor(Point nei) {
                neighbors.add(nei);
        }

        public int activeCount(){
                int ile = 0;
                for(Point p1 : neighbors){
                        if(p1.currentState >=2 && p1.currentState < 6 ){
                                ile++;
                        }
                }
                return ile;
        }
        
        public double temp(double te)
        {
        	double tmp=0.0;
        	if(te>=24)
        		tmp=0.005;
        	while(te<15)
        	{
        			te=te+1;
        			tmp=tmp-0.003;
        	}
        	while(te>15)
        	{
        			te=te-1;
        			tmp=tmp+0.003;
        	}
        	return tmp;
        }
        public double humadity_a(double air)
        {
        	double tmp=0;
        	if(air<=40)
        		tmp=0.001;
        	while(air>60)
        	{
        			air=air-1;
        			tmp=tmp-0.0011;
        	}
        	while(air<60)
        	{
        			air=air+1;
        			tmp=tmp+0.001;
        	}
        	return tmp;
        }
        public double wiatr(int wind){
                double ile = 0.0;
                if(wind==1){
                        for(Point p1 : neighbors){
                                if(p1.currentState >= 2 && p1.y==this.y+1){
                                        ile=ile+0.05;
                                }
                                if(p1.currentState >= 2 && p1.y==this.y-1){
                                        if (ile==0) 
                                                ile=-0.02;
                                }
                        }
                }else if(wind == 2){
                        for(Point p1 : neighbors){
                                if(p1.currentState>=2 && p1.y==this.y-1){
                                        ile=ile+0.05;
                                }else if(p1.currentState>=2 && p1.y==this.y+1){
                                        if(ile==0) 
                                                ile=-0.02;
                                }
                        }
                }else if(wind == 3){
                        for(Point p1 : neighbors){
                                if(p1.currentState>=2 && p1.x==this.x+1){
                                        ile=ile+0.05;
                                }else if(p1.currentState>=2 && p1.x==this.x-1){
                                        if(ile==0) 
                                                ile=-0.02;
                                }
                        }
                }else if(wind == 4){
                        for(Point p1 : neighbors){
                                if(p1.currentState>=2 && p1.x==this.x-1){
                                        ile=ile+0.05;
                                }else if(p1.currentState>=2 && p1.x==this.x+1){
                                        if(ile==0) 
                                                ile=-0.02;
                                }
                        }
                }else if(wind == 5){
                        for(Point p1 : neighbors){
                                if(p1.currentState>=2 && p1.y==this.y+1 && p1.x==this.x+1){
                                        ile=ile+0.05;
                                }else if(p1.currentState>=2 && p1.y==this.y-1 && p1.x==this.x-1){
                                        if(ile==0) 
                                                ile=-0.02;
                                }
                        }
                }else if (wind == 6){
                        for(Point p1 : neighbors){
                                if(p1.currentState>=2 && p1.y==this.y+1 && p1.x==this.x-1){
                                        ile=ile+0.05;
                                }else if(p1.currentState>=2 && p1.y==this.y-1 && p1.x==this.x+1){
                                        if(ile==0) 
                                                ile=-0.02;
                                }
                        }
                }else if (wind == 7){
                        for(Point p1 : neighbors){
                                if(p1.currentState>=2 && p1.y==this.y-1 && p1.x==this.x+1){
                                        ile=ile+0.05;
                                }else if(p1.currentState>=2 && p1.y==this.y+1 && p1.x==this.x-1){
                                        if(ile==0)
                                                ile=-0.02;
                                }
                        }
                }else if (wind == 8){
                        for(Point p1 : neighbors){
                                if(p1.currentState>=2 && p1.y==this.y-1 && p1.x==this.x-1){
                                        ile=ile+0.05;
                                }else if(p1.currentState>=2 && p1.y==this.y+1 && p1.x==this.x+1){
                                        if(ile==0) 
                                                ile=-0.02;
                                }
                        }
                }
        return ile;
        }
}
